<template>
  <div>
    <el-form label-width="80px" ref="form" :rules="rules" :model="form">
      <el-form-item prop="science_name" label="标题:">
        <el-input
          placeholder="请输入科普标题"
          v-model="form.science_name"
        ></el-input>
      </el-form-item>
      <el-form-item prop="science_type" label="种类:">
        <el-input
          placeholder="请输入科普标题"
          v-model="form.science_type"
        ></el-input>
      </el-form-item>
      <el-form-item label="图片:" prop="science_pic">
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:9000/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.science_pic" :src="form.science_pic" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onsubmit">点击新增</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import httpApi from "@/http";
export default {
  components: { Editor, Toolbar },
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      form: {
        science_name: "",
        science_pic: "",
        science_type:''
      },
      rules: {
        science_name: [{ required: true, message: "必填", trigger: "blur" }],
        science_pic: [{ required: true, message: "必填", trigger: "blur" }],
        science_type: [{ required: true, message: "必填", trigger: "blur" }]
      },
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.form.science_pic = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    onsubmit() {
      console.log(this.form);
      this.$refs.form.validate((valid) => {
        if (valid) {
          httpApi.scienceApi.add(this.form).then((res) => {
            console.log("新增科普", res);
            // 跳转到列表页面
            this.$router.push("/home/science-list");
          });
        }
      });
    },
  },
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;

  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  display: block;
}
</style>
